package com.magi.builder;

import com.magi.constant.DefaultMapperID;
import com.magi.core.AppUtils;
import org.dom4j.Element;

import java.io.IOException;

public class XmlMapperBuilder implements IXmlMapperBuilder {
    private final XmlFileBuilder xmlFileBuilder;

    public static String DEFAULT_DELETE_ID = DefaultMapperID.DELETE;
    public static String DEFAULT_UPDATE_ID = DefaultMapperID.UPDATE;
    public static String DEFAULT_SELECT_ID = DefaultMapperID.SELECT;
    public static String DEFAULT_INSERT_ID = DefaultMapperID.INSERT;

    public XmlMapperBuilder(String namespace) {
        this.xmlFileBuilder = new XmlFileBuilder("mapper");
        this.xmlFileBuilder.getRoot().addAttribute("namespace", namespace);
        xmlFileBuilder.addDocType("mapper",
                "-//mybatis.org//DTD Mapper 3.0//EN",
                "http://mybatis.org/dtd/mybatis-3-mapper.dtd"
                );
    }

    @Override
    public void addSelect(String id, String parameterType, String resultType, String sql) {
        this.addStatement(DEFAULT_SELECT_ID, id, parameterType, resultType, sql);
    }

    @Override
    public void addInsert(String id, String parameterType, String resultType, String sql) {
        this.addStatement(DEFAULT_INSERT_ID, id, parameterType, resultType, sql);
    }

    @Override
    public void addDelete(String id, String parameterType, String resultType, String sql) {
        this.addStatement(DEFAULT_DELETE_ID, id, parameterType, resultType, sql);
    }

    @Override
    public void addUpdate(String id, String parameterType, String resultType, String sql) {
        this.addStatement(DEFAULT_UPDATE_ID, id, parameterType, resultType, sql);
    }

    private void addStatement(String name, String id, String parameterType, String resultType, String sql) {
        if (id == null || id.isEmpty()) { throw new RuntimeException("select id could not be null or empty"); }
        if (name == null || name.isEmpty()) { throw new RuntimeException("statement name could not be null or empty"); }
        if (sql == null || sql.isEmpty()) { throw new RuntimeException("sql could not be null or empty"); }

        Element select = xmlFileBuilder.getRoot().addElement(name)
                .addAttribute("id", id)
                .addText(Identifiers.CRLF)
                .addText(sql)
                .addText(Identifiers.CRLF);
        if (parameterType != null && !parameterType.isEmpty()) { select.addAttribute("parameterType", parameterType); }
        if (resultType != null && !resultType.isEmpty()) { select.addAttribute("resultType", parameterType); }
    }

    @Override
    public String text() {
        return xmlFileBuilder.text();
    }

    @Override
    public void writeToFile(String filename) throws IOException {
        AppUtils.writeToFile(filename, this.text());
    }

    public static void main(String[] args) {
        XmlMapperBuilder xmlMapperBuilder = new XmlMapperBuilder("test");
        xmlMapperBuilder.addSelect("add", null, null, "SELECT * FROM #{table}");
        System.out.println(xmlMapperBuilder.text());
    }
}
